package karileet.meeting.profile.fragment;
import java.util.ArrayList;

import karileet.meeting.profile.ProfileConf;
import karileet.meeting.profile.R;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;

public class WeekdayFragment extends DialogFragment {
	private String weekdays = "";
	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		
		Resources res = getResources();
	    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
	    // Get the layout inflater
	    LayoutInflater inflater = getActivity().getLayoutInflater();
//	    getDialog().setTitle("Notification settings");
	    builder.setTitle(res.getString(R.string.title_weekdays));
	    // Inflate and set the layout for the dialog
	    // Pass null as the parent view because its going in the dialog layout
	    View v = inflater.inflate(R.layout.weekday_fragment, null);
	    final ProfileConf state = new ProfileConf(getActivity());
	    final ArrayList<CheckBox> cbs = new ArrayList<CheckBox>();
	    
	    cbs.add((CheckBox) v.findViewById(R.id.monday));
    	cbs.add((CheckBox) v.findViewById(R.id.tuesday));
    	cbs.add((CheckBox) v.findViewById(R.id.wednesday));
    	cbs.add((CheckBox) v.findViewById(R.id.thursday));
    	cbs.add((CheckBox) v.findViewById(R.id.friday));
    	cbs.add((CheckBox) v.findViewById(R.id.saturday));
    	cbs.add((CheckBox) v.findViewById(R.id.sunday));

    	preSetWeekdays(cbs);
    	
	    builder.setView(v)
	    // Add action buttons
	           .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
	               @Override
	               public void onClick(DialogInterface dialog, int id) {
	            	   int i = 2;
	            	   //read contents of all checkboxes and append all day numbers that are selected
	            	   for(CheckBox c : cbs) {
	            		   if (c.isChecked()) {
	            			   weekdays += i + ";"; 
	            		   }
	            		   i++;
	            		   if(i == 8) i = 1;
	            	   }
	            	   Log.d(ProfileConf.APP, "Storing weekdays: " + weekdays);
	            	   state.setWeekdays(weekdays);
	               }
	           }).setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
	               @Override
	               public void onClick(DialogInterface dialog, int id) {
	               }
	           });   
	    return builder.create();
	}
	
	private void preSetWeekdays(ArrayList<CheckBox> cbs) {
		Log.d(ProfileConf.APP, "pre-setting weekdays");
		ProfileConf state = new ProfileConf(getActivity());
		String weekdays = state.getWeekdays();
		Log.d(ProfileConf.APP, "weekdays:" +weekdays);
		int i = 2;
		for (CheckBox c : cbs) {
			if(weekdays.indexOf(Integer.toString(i)) != -1) {
				c.setChecked(true);
			}
			i++;
			if(i == 8) i = 1;
		}
		
	}
}
